import Foods from "./Foods";
import Snake from "./Snake";
import ScorePanel from "./ScorePanel";
import WebBtn from "./WebBtn";

/**
 * 游戏的控制器
 */
class GameController {
    public snake:Snake;
    public food:Foods;
    public scorePanel:ScorePanel;

    public direction: String = "ArrowRight" // 移动方向
    public isLive:Boolean = false // 游戏是否结束
    public webBtn:WebBtn; // 上下左右的按钮
    constructor() {
        this.snake = new Snake()
        this.food = new Foods()
        this.scorePanel = new ScorePanel()

        // 给按钮绑定响应函数
        this.webBtn = new WebBtn();
        this.webBtn.upButton.addEventListener("click", (e:Event) => this.upButton());
        this.webBtn.leftButton.addEventListener("click", (e:Event) => this.leftButton());
        this.webBtn.rightButton.addEventListener("click", (e:Event) => this.rightButton());
        this.webBtn.downButton.addEventListener("click", (e:Event) => this.downButton());

        // 初始化控制器
        this.init()
    }

    init(){
        this.beginGame();
        document.addEventListener("keydown", this.keyDownHandler.bind(this))
        this.run()
    }

    /**
     * 按钮响应函数
     *
     * @param   {KeyboardEvent}  event  按钮事件
     */
    keyDownHandler(event: KeyboardEvent){
        let { key } = event
        /*
            ArrowUp     Up
            ArrowDown   Down
            ArrowLeft   Left
            ArrowRight  Right
         */
        if(this.getLegalDirection().includes(key)){
            this.direction = event.key;
        }
    }

    beginGame(){
        this.isLive = true
    }

    /**
     * 开始控制器的方法
     */
    run(){
        // 吃到食物
        if(this.checkEat()){
            this.food.change()
            this.scorePanel.addScore()
            this.snake.addBody()
        }

        switch (this.direction) {
            case 'ArrowUp':
            case  'Up':
                this.snake.Y -= 10
                break
            case 'ArrowDown':
            case  'Down':
                this.snake.Y += 10
                break
            case 'ArrowLeft':
            case  'Left':
                this.snake.X -= 10
                break
            case 'ArrowRight':
            case  'Right':
                this.snake.X += 10
                break
        }
        
        // 撞到自己的身体
        if(this.snake.touchBody()){
            this.isLive = false
            alert("游戏结束！")
        }
        this.isLive && setTimeout(this.run.bind(this), 300 - (this.scorePanel.level - 1) * 25)

    }

    /**
     * 检测是否吃到食物
     */
    checkEat(){
        return this.food.X === this.snake.X && this.food.Y === this.snake.Y;
    }

    /**
     * 获取有效方向的值
     * 
     * @return  {[String]}  有效方向的值
     */
    getLegalDirection(){
        return[
            'ArrowUp',
            'ArrowDown',
            'ArrowLeft',
            'ArrowRight',
            'Up',
            'Down',
            'Left',
            'Right'
        ]
    }

    
    /**
     * 页面上的按钮响应函数
     */
    private upButton(){
        this.direction = "ArrowUp"
    }
    /**
     * 页面上的按钮响应函数
     */
    private leftButton() {
        this.direction = "ArrowLeft"
    }
    /**
     * 页面上的按钮响应函数
     */
    private rightButton() {
        this.direction = "ArrowRight"
    }
    /**
     * 页面上的按钮响应函数
     */
    private downButton() {
        this.direction = "ArrowDown"
    }
}


export default GameController
